# -*- coding:utf-8 -*-
# @Author:Cheng Lei 1037654919@qq.com
# @Time : 2023/3/24 下午1:55
# @FileName: kuwo_t3_song.py
# @Software: PyCharm
# 第三步 获取所有歌曲信息

from multiprocessing import Pool
from class_kuwo import Kuwo
kuwo = Kuwo()
from utils import MongoDBUtil, get_html, mongo_manager
MongoDBUtil = MongoDBUtil()
playlist_tag = mongo_manager("playlist_tag", db="kuwo")
playlist_song= mongo_manager("playlist_song", db="kuwo")
singer_song= mongo_manager("singer_song", db="kuwo")

playlist_song_list= mongo_manager("playlist_song_list", db="kuwo")


# 基于歌单更新歌曲列表
def playlist_song_list_run():
    seeds = playlist_song.findAll({"status": None})
    lists =[]
    for seed in seeds:
        lists.append(seed)
    print('playlist_song_list None:',len(lists))
    for seed in seeds:
        result ={"_id" : seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status']='success'
        playlist_song.updateOne({'_id': seed['_id']}, seed)
# 基于歌手更新歌曲列表
def singer_song_list_run():
    seeds = singer_song.findAll({"status": None})
    lists =[]
    for seed in seeds:
        lists.append(seed)
    print('singer_song_list_run None:',len(lists))
    for seed in seeds:
        result ={"_id":seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status']='success'
        singer_song.updateOne({'_id': seed['_id']}, seed)

# 插入数据
def insert_data(tablename, datas):
    if len(datas) > 0 and datas is not None:
        for data in datas:
            # data['_id'] = data['id']
            try:
                # print(data)
                MongoDBUtil.insert_one(tablename, data)
            except Exception as e:
                print(e)
    else:
        pass


# 解析歌曲信息
def parse_song(data):
    datas = data
    result = {'_id': datas.get("rid"), 'musicrid': datas.get("musicrid"),
              'name': datas.get("name"), 'score100': datas.get("score100"),
              'img': datas.get("pic"), 'album': datas.get("album"),
              'albumid': datas.get("albumid"), 'albuminfo': datas.get("albuminfo"),
              'albumpic': datas.get("albumpic"),'artist': datas.get("artist"),
              'artistid': datas.get("artistid"), 'barrage': datas.get("barrage"),
              'content_type': datas.get("content_type"), 'duration': datas.get("duration"),
              'hasLossless': datas.get("hasLossless"), 'hasmv': datas.get("hasmv"),
              'isListenFee': datas.get("isListenFee"), 'isstar': datas.get("isstar"),
              'mvPlayCnt': datas.get("mvPlayCnt"), 'mvUpPcStr': datas.get("mvUpPcStr"),
              'online': datas.get("online"), 'originalsongtype': datas.get("originalsongtype"),

              'releaseDate': datas.get("releaseDate"),
              'songTimeMinutes': datas.get("songTimeMinutes"), 'tme_musician_adtype': datas.get("tme_musician_adtype"),
              'track': datas.get("track"), 'upPcStr': datas.get("upPcStr")
              }
    return result


# 获取歌曲信息
def get_music_info(seed):
    playlist_song_list = mongo_manager("playlist_song_list", db="kuwo")
    mid = seed['_id']

    datas = kuwo.get_music_info(mid=mid)
    results = parse_song(datas)
    lyric = kuwo.get_song_lyric(mid=mid)  # list
    results['lyric'] = lyric
    insert_data(tablename='song_info', datas=[results])
    seed['status'] = 'success'
    playlist_song_list.updateOne({'_id': seed['_id']}, seed)
    playlist_song_list.close()
    # return seeds

# 获取歌曲信息 run
def get_music_info_run():
    seeds = playlist_song_list.findAll({"status": None})
    lists =[]
    for seed in seeds:
        lists.append(seed)
    print('playlist_song_list None:',len(lists))
    playlist_song_list.close()
    pool = Pool(processes=32)
    pool.map(get_music_info, lists)
    pool.close()  # 关闭进程池，不再接受新的进程
    pool.join()



if __name__ == '__main__':

    # # run
    # 基于歌单更新歌曲列表
    playlist_song_list_run()
    # 基于歌手更新歌曲列表
    # singer_song_list_run()
    seeds = singer_song.findAll({"status": None})
    lists =[]
    for seed in seeds:
        lists.append(seed)
    print('singer_song_list_run None:',len(lists))
    seeds = singer_song.findAll({"status": None})
    for seed in seeds:
        result ={"_id":seed['rid']}
        try:
            # print(data)
            MongoDBUtil.insert_one('playlist_song_list', result)
        except Exception as e:
            print(e)
        seed['status']='success'
        singer_song.updateOne({'_id': seed['_id']}, seed)
    # 检查运行情况

    # # 下载歌曲信息
    # get_music_info_run()

    pass
